#!/bin/bash

#------------------------------------------------------------------------------
#Filename:	74741-nfs.sh
#Version:	1.1
#Date:		2013/07/15
#Author:	yuanhui.shi
#Email:		yuanhui.shi@cs2c.com.cn
#Summary:	02.01系统核心功能-02目录管理-02目录共享-01测试nsf目录共享功能
#Notes:		验证nfs服务器能否搭建成功
#Copyright:	China Standard Software Co.,Ltd.
#History:
#		Version 1.0,2013/07/11
#		-The first one
#		Version 1.1,2013/09/03
#		-Modify the script to satisfy the shell standard
#		Version 1.2,2013/09/05
#       	-Set user\ip\password as variable
#------------------------------------------------------------------------------


PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

source ../../lib/Echo.sh
source ./lib/Ssh.sh
source ../../lib/XmlParse.sh

function CleanData ()
{
  cd ..
  umount /mnt
  EchoInfo "关闭服务器端的nfs服务，删除共享目录"
  Ssh "${USER}" "${IP}" "${PASSWORD}" "service nfs stop"
  Ssh "${USER}" "${IP}" "${PASSWORD}" "rm -rf /share"
  Ssh "${USER}" "${IP}" "${PASSWORD}" "echo '' >/etc/exports"
  EchoInfo "74741-nfs.sh执行完毕"
}

trap "CleanData" EXIT

DATA_DIR="./data"
DATA_FILE="74741-nfs.xml"

XmlParse ${DATA_DIR}/${DATA_FILE}

GetValue USER USER
GetValue IP IP
GetValue PASSWORD PASSWORD

EchoInfo "远程配置服务器${IP}"

Scp "${USER}" "${IP}" "${PASSWORD}" "./nfs-config.sh" "/root/"

Ssh "${USER}" "${IP}" "${PASSWORD}" "sh nfs-config.sh"

Ssh "${USER}" "${IP}" "${PASSWORD}" "echo 查看服务器端nfs共享目录清单"

Ssh "${USER}" "${IP}" "${PASSWORD}"  "showmount -e 127.0.0.1"

Ssh "${USER}" "${IP}" "${PASSWORD}"  "rm -rf /root/nfs-config.sh"

EchoInfo "客户端（本机）开始检验nfs服务器是否配置成功"
service iptables stop &> /dev/null
test -n "`showmount -e ${IP} |grep share`" 
EchoResult "客户端可以看见nsf共享目录"

mount ${IP}:/share /mnt
cd /mnt

EchoInfo "检验客户端能否新建文件夹"
#test `mkdir dir1`  
#有没有test影响很大，有test，得到的值是1
mkdir dir1
EchoResult "新建文件夹操作成功" 

EchoInfo "检验客户端能否新建文件"
touch file1 
EchoResult "新建文件操作成功" 

EchoInfo "检验客户端能否修改文件"
echo "This is a test!" > file1
test "`cat file1`" == "This is a test!" 
EchoResult "修改文件操作成功" 

EchoInfo "检验客户端能否删除文件夹"
rmdir dir1 
EchoResult "删除文件夹操作成功" 

EchoInfo "检验客户端能否删除文件"
rm -rf file1 
EchoResult "删除文件操作成功" 
